package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjDictionary;
import cn.stylefeng.guns.modular.mapper.IdjDictionaryMapper;
import cn.stylefeng.guns.modular.service.IIdjDictionaryService;
import cn.stylefeng.roses.core.util.ToolUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-30
 */
@Service
public class IdjDictionaryServiceImpl extends ServiceImpl<IdjDictionaryMapper, IdjDictionary> implements IIdjDictionaryService {

	@Autowired
	private IdjDictionaryMapper mapper;
	
	@Override
	public LayuiPageInfo getList(Page<IdjDictionary> page, String dictionaryName) {
		Page<Map<String,Object>> list = mapper.getList(page, dictionaryName);
		return LayuiPageFactory.createPageInfo(list);
	}

	@Override
	public ModelAndView edit(Long id) {
		IdjDictionary dict = mapper.selectByKeyId(id);
		if(dict != null){
			if(ToolUtil.isNotEmpty(dict.getDictionaryData())){
				List<IdjDictionary> dictList = new ArrayList<IdjDictionary>();
				String[] split = dict.getDictionaryData().split(",");
				dict.setDictionaryData(split[0]);
				for (int i = 1; i < split.length; i++) {
					IdjDictionary dicts = new IdjDictionary();
					dicts.setDictionaryData(split[i]);
					dicts.setDictionaryName(dict.getDictionaryName());
					dicts.setId(dict.getId());
					dictList.add(dicts);
				}
				dict.setDictList(dictList);
			}
		}
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("dict", dict);
        return modelAndView;
	}

	@Override
	public int update(IdjDictionary dict) {
		return mapper.update(dict);
	}

	@Override
	public int insert(IdjDictionary dict) {
		return mapper.insert(dict);
	}

	@Override
	public int delete(Long id) {
		return mapper.delete(id);
	}

	@Override
	public List<String> selectDictionaryName() {
		return mapper.selectDictionaryName();
	}

}
